<?php
/**
* 鍏辩敤鍑芥暟
*
* 鏈▼搴忎富瑕佷綔鐢ㄥ畾涔夌郴缁熷叡鐢ㄧ殑鍑芥暟
* 
* @category   Common
* @package    Common
* @copyright  Copyright (c) ink宸ヤ綔瀹�(http://www.inkphp.com)
* @author     寮犳棴鍒�
* @version    v1.0 beta
*/

/**
 * 鍙栧緱妯℃澘鏂囦欢鐨勮矾寰勶紝鐢ㄥ湪妯℃澘鏂囦欢涓寘鍚叾浠栨ā鏉挎枃浠剁殑鏃跺�
 * @param type $name 妯℃澘鍚嶇О
 * @return string 杩斿洖鍏ㄨ矾寰剆vn
 */
function getTplPath($name){
    $path = './Theme/'.__DEFAULT_THEME__.'/'.APP_NAME.'/'.$name.'.tpl.php';
    return $path;
}

/**
 * 鐢ㄦ潵澶勭悊璺宠浆璺緞
 * @param type $router 閾炬帴鍦板潃
 * @param type $data 瑕佷紶閫掔殑鍙傛暟
 * @return string 瀹屾暣閾炬帴鍦板潃
 */
function U($router,$data = null){
    if(substr($router,0,7) == 'http://'){
        return $router;
    }
    $r = explode('?',$router);
    if(count($r) > 1){
        $router = $r[0];
        $s = $r[1];
        $d[$s] = $data;
        $data = $d;
    }
    if(__USE_ROUTER__){
        $d = explode('/',$router);
        $rules = include('./core/Common/router.inc.php');
        $isrule = false;
        foreach($rules as $key => $value){
            if($d[0] == $value['app'] && $d[1] == $value['m'] && $d[2] == $value['a']){
                $isrule = true;
                $router = $key;
                break;
            }
        }
        
        if(!$isrule){
            $router = str_replace('/', '_', $router);
        }
        
        $router = '/'.$router;
        if(!empty($data)){
            foreach($data as $k => $v){
                $str .= '/'.$k.'/'.$v;
            }
        }
        $router = __BASE_PATH__.$router;
        $router .= '.html';
    }else{
		$rules = include('./core/Common/router.inc.php');
		if(empty($rules[$router])){
			$d = explode('/',$router);
		}else{
			$d[0] = $rules[$router]['app'];
			$d[1] = $rules[$router]['m'];
			$d[2] = $rules[$router]['a'];
		}
        $router = './index.php?app='.$d[0].'&m='.$d[1].'&a='.$d[2];
        foreach((array)$data as $k => $v){
            $str .= '&'.$k.'='.$v;
        }
        $router .=$str;
    }
    return $router;
}

/**
 * 鐢ㄦ潵瀵煎叆CSS鍜孞S鏂囦欢锛屽彲浠ラ噰鐢ㄤ袱绉嶆柟寮忥紝涓�鏄互鏂囦欢褰㈠紡瀵煎叆锛屼竴绉嶆槸浠ヤ唬鐮佸舰寮忓鍏ワ紝鍏蜂綋鍙傝�閰嶇疆鏂囦欢涓殑甯搁噺__CACHE_FORWARD_FILE__
 * @param type $name 瀵煎叆鏂囦欢鐨勫悕瀛�
 * @param type $type 瀵煎叆鏂囦欢鐨勬墿灞曞悕
 */
function S($name = 'base',$type = 'css'){
    $pre = substr($name,0,3);
    if(!__USE_ROUTER__){
        if($pre == 'sys'){
            $path = __CORE__.'/static/Public/'.$type.'/'.$name.'.'.$type;
        }else{
            $path .= './static/'.__DEFAULT_THEME__.'/'.APP_NAME.'/'.$type.'/'.$name.'.'.$type;
        }
    }else{
        if($pre == 'sys'){
            $path = __SITENAME__.'/core/static/Public/'.$type.'/'.$name.'.'.$type;
        }else{
            $path .= __SITENAME__.'/static/'.__DEFAULT_THEME__.'/'.APP_NAME.'/'.$type.'/'.$name.'.'.$type;
        }
    }
//    if(file_exists($path)){
        if(!__CACHE_FORWARD_FILE__){
            $str = '?'.rand(1000000,9999999);
            $path .= $str;
        }
        if(!__LOAD_FILE__){
            if($type == 'css'){
                $code = '<style type="text/css">'."\n";
                $code .= file_get_contents($path);
                $code .= '</style>'."\n";
            }elseif($type == 'js'){
                $code = '<script language="javascript">'."\n";
                $code .= file_get_contents($path);
                $code .= '</script>'."\n";
            }
            echo $code;
        }else{
            if($type == 'css'){
                echo '<link rel="stylesheet" href="'.$path.'" type="text/css" />'."\n";
            }elseif($type == 'js'){
                echo '<script language="javascript" src="'.$path.'"></script>'."\n";
            }
        }
//    }
}

/**
 * 鍥介檯鍖栨柟娉曪紝鐢ㄦ潵瀵绘壘鍚勭璇█鍖咃紝骞跺皢涔嬬炕璇戞樉绀哄湪椤甸潰涓�
 * @param type $name 瑕佺炕璇戠殑瀛楃涓�
 * @return type 
 */
function L($name){
    $path = './core/Lang/'.__DEFAULT_LANG__.'/language.php';
    if(!file_exists($path)){
        return getLang($name);
    }else{
        $lang = include($path);
        if(!empty($lang[$name])){
            return $lang[$name];
        }else{
            return getLang($name);
        }
    }
}
/**
 * 鐢ㄦ潵瀵绘壘鐢ㄦ埛鑷畾涔夌殑鍥介檯鍖栨枃浠�
 * @param type $name 瑕佺炕璇戠殑瀛楃涓�
 * @return type 缈昏瘧涔嬪悗鐨勫瓧绗︿覆锛屽鏋滄病鏈夋壘鍒拌繑鍥炲師瀛楃涓�
 */
function getLang($name){
    $path = './Lang/'.__DEFAULT_LANG__.'/'.APP_NAME.'/language.php';
    if(!file_exists($path)){
        return $name;
    }else{
        $lang = include($path);
        if(!empty($lang[$name])){
            return $lang[$name];
        }else{
            return $name;
        }
    }
}

function checkPhoto($path){
    if($path == ''){
        $path = './static/default/Admin/images/nopic.png';
    }elseif(!file_exists ($path)){
        $path = './static/default/Admin/images/nopic.png';
    }
    return $path;
}

/**
 * 鑷姩鍔犺浇鍏辩敤Controller,Model鐨勬柟娉�
 * @param type $name Controller鐨勫悕瀛�
 */
function __autoload($name){
    if(isModel($name)){
        include('./Model/'.$name.'.class.php');
    }else{
        include('./Apps/'.APP_NAME.'/Controller/'.$name.'.class.php');
    }
}

function isModel($name){
    $n = str_replace('Model','',$name);
    if($n != $name){
        return true;
    }
    return false;
}

function Plugin($name){
    $str = Core::loagPlugin($name);
    echo $str;
}

function doEncrypt($input, $key){
//    $key = substr(md5($key), 0, 24);
    $td = mcrypt_module_open('tripledes', '', 'ecb', '');
    $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
    mcrypt_generic_init($td, $key, $iv);
    $encrypted_data = mcrypt_generic($td, $input);
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);
    return base64_encode($encrypted_data);
}

function doDecrypt($input, $key){
    $input = base64_decode($input);
    $td = mcrypt_module_open('tripledes', '', 'ecb', '');
//    $key = substr(md5($key), 0, 24);
    $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
    mcrypt_generic_init($td, $key, $iv);
    $decrypted_data = mdecrypt_generic($td, $input);
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);
    return $decrypted_data;

}

function getParams(){
    $str = $_SERVER['QUERY_STRING'];
    if(!empty($str)){
        $data = explode('&', $str);
        foreach($data as $k => $v){
            $arr = explode('=', $v);
            $d[$arr[0]] = urldecode($arr[1]);
        }
        return $d;
    }else{
        return null;
    }
}

function processUrl($url){
    $url = str_replace('.html','',$url);
    $urls = explode('?',$url);//str_replace('?','/',$url);
    $url = $urls[0];
    $urls = explode('/',$url);
    $len = count($urls);
    $urls[$len-1] = str_replace('_', '/', $urls[$len-1]);
    $url = implode('/', $urls);
    $url = str_replace(__BASE_PATH__,'',$url);
    return $url;
}